﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;
using System.Net.Mail;




namespace Nivdal
{
    public partial class SendInvitation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.SendInvitation_PageTitle;

            if (!IsPostBack)
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                OnlineUser ou = (OnlineUser)Session["User"];

                YourNameTextBox.Text = GND.ReadNivdalStringData("UserId", "Nivdal_GetUserRealFirstName", ou.UserId) + " " +
                                       GND.ReadNivdalStringData("UserId", "Nivdal_GetUserRealLastName", ou.UserId);

            }

        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string RegisterId = GND.ReadNivdalStringData("UserId", "Nivdal_GetRegisterId", ou.UserId);
            string InviterName = YourNameTextBox.Text.Trim();
            if (InviterName.Length == 0)
                InviterName = ou.UserName;

            GND.ExecNivdalSQL("UserId", "Destination", "Language", "PersonalMessage", "FriendName", "YourName",
                              "Nivdal_CreateInvitationMail",
                              ou.UserId, FriendEmailAddressTextBox.Text.Trim(),
                              LanguageDropDownList.SelectedValue,
                              PersonalMessageTextBox.Text.Trim(),
                              FriendNameTextBox.Text.Trim(),
                              InviterName);

            GND.SendPendingMails();

            lblMessage.Text = Resources.Resource.SendInvitation_SentOk;

            FriendEmailAddressTextBox.Text = "";
            FriendNameTextBox.Text = "";
            ////this.lblMessage.Text = "RegisterId is: " + RegisterId + "UserId is: " + ou.UserId;
            //string MailSender = "admin@nivdal.com";
            //try
            //{

            //    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            //    message.Subject = InviterName + " " + Resources.Resource.SendInvitation_MailTile;
            //    message.Sender = new System.Net.Mail.MailAddress(MailSender);
            //    message.To.Add(FriendEmailAddressTextBox.Text.Trim());
            //    message.Body = Resources.Resource.SendInvitation_MailBody + Environment.NewLine + Environment.NewLine +
            //                   PersonalMessageTextBox.Text.Trim() + Environment.NewLine + Environment.NewLine +
            //                   "http://www.nivdal.com/Register.aspx?RegisterId=" + RegisterId;
            //    message.BodyEncoding = System.Text.Encoding.UTF8;
            //    //System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("127.0.0.1");
            //    SmtpClient smtp = new SmtpClient();
            //    // gmail only - remove later
            //    //smtp.EnableSsl = true;
            //    smtp.Send(message);
            //    lblMessage.Text = Resources.Resource.SendInvitation_SentOk;

            //    FriendEmailAddressTextBox.Text = "";
            //    FriendNameTextBox.Text = "";

            //}
            //catch (Exception exc)
            //{
            //    lblMessage.Text = (Resources.Resource.Contact_SendFailure + " " + exc.ToString());
            //}


        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }
    }
}